//引入http模块
const http = require('http');
const url = require('url');
const path = require('path');
const fs = require('fs');
const mime = require('mime');
//创建网站服务器对象
const app = http.createServer();
app.on('request',(req, res)=>{
    let pathname = url.parse(req.url).pathname;//自带/
    pathname = pathname == '/'? '/index.art' : pathname;
    let realpath = path.join(__dirname,'public'+pathname);
    let type = mime.getType(realpath);
    console.log(type);
    fs.readFile(realpath,(err,doc)=>{
        if (err != null){
            res.writeHead(404,{
                'content-type': 'text/html;charset=utf8'
            });
            res.end('文件读取失败');
            return;
        }
        res.writeHead(200,{
            'content-type' : type
        })
        res.end(doc);
    })
});
//监听窗口，没有这个无法访问
app.listen(3000);
console.log('网站服务器启动成功')